package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import dao.NoticeDao;
import dao.NoticeDaoFactory;
import entity.Notice;

/*
 * 作用：搜索内容
 */
@WebServlet("/QueryContentServlet")
public class QueryContentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.接受客户端发送过来参数
		String queryContent = request.getParameter("queryContent");
		
		//2.通过service(dao)获取结果
		NoticeDao dao = new NoticeDaoFactory().factory();
		List<Notice> noticelist = dao.queryNoticeByContent(queryContent);
		//3.把数据包装成一个API，返回成json字符串
		Gson gson = new GsonBuilder()
					.setDateFormat("yyyy-MM-dd HH:mm:ss")
					.create();
		String json = gson.toJson(noticelist);
		
		//然后输出
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//response.setContentType("text/html;charset=utf-8");
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		
		//3.不需要跳转，把结果打包成json，然后输出给客户端
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
